import express from 'express';
import db from '../config/db.js';
const router = express.Router();

// 获取训练记录
router.get('/trainings', async (req, res) => {
  try {
    console.log('获取训练记录请求');
    
    // 这里可以根据用户ID过滤，暂时获取所有记录
    const [rows] = await db.pool.promise().execute(`
      SELECT 
        id,
        title,
        category,
        price,
        level,
        duration,
        durationRange,
        equipment,
        cover as img,
        NOW() as created_at
      FROM trainings 
      ORDER BY id DESC
    `);
    
    console.log('训练记录查询结果:', rows);
    res.json(rows);
  } catch (error) {
    console.error('获取训练记录失败:', error);
    res.status(500).json({ error: '获取训练记录失败' });
  }
});

// 获取我的预定
router.get('/mytrainings', async (req, res) => {
  try {
    console.log('获取我的预定请求');
    
    // 这里可以根据用户ID过滤，暂时获取所有记录
    const [rows] = await db.pool.promise().execute(`
      SELECT 
        id,
        name as title,
        level,
        equipments,
        parts,
        warmup,
        stretch,
        createdAt,
        '/image/nv1.jpg' as img
      FROM mytrainings 
      ORDER BY createdAt DESC
    `);
    
    console.log('我的预定查询结果:', rows);
    res.json(rows);
  } catch (error) {
    console.error('获取我的预定失败:', error);
    res.status(500).json({ error: '获取我的预定失败' });
  }
});

export default router;
